package kr.pe.johnkim.carrotpoint.mobile.controller;

import kr.pe.johnkim.carrotpoint.mobile.domain.CarrotPointMerchantStock;
import kr.pe.johnkim.carrotpoint.mobile.service.CarrotPointMerchantStockService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Carrot Point 가맹점(입점업체) 상품 관련 controller 클래스 이다.
 *
 * @author : John_Kim <cale9797@gmail.com>
 * @date : 13. 2. 6.
 */
@Controller
@RequestMapping(value = "/carrotPointMerchantStockController")
public class CarrotPointMerchantStockController {

    // SLF4J Logging
    private Logger logger = LoggerFactory.getLogger(CarrotPointMerchantStockController.class);

    @Autowired
    private CarrotPointMerchantStockService carrotPointMerchantStockService;

    /**
     * 가맹점(입점업체) 상품을 등록 한다.
     *
     * @param carrotPointMerchantStock
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/registerMerchantStock", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public CarrotPointMerchantStock registerMerchantStock
            (@RequestBody CarrotPointMerchantStock carrotPointMerchantStock) throws Exception {
        logger.info("registerMerchantStock");

        CarrotPointMerchantStock result = new CarrotPointMerchantStock();

        try {
            carrotPointMerchantStockService.registerMerchantStock(carrotPointMerchantStock);
            result.setResult("SUCCESS");
            logger.info("success to register merchant stock.");
        } catch (Exception e) {
            result.setResult("FAIL");
            logger.error("fail to register merchant stock.");
        }

        return result;
    }

    /**
     * 가맹점(입점업체) 상품 목록을 가져 온다.
     *
     * @param carrotPointMerchantStock
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getMerchantStockList", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public List<CarrotPointMerchantStock> getMerchantStockList
            (@RequestBody CarrotPointMerchantStock carrotPointMerchantStock) throws Exception {
        logger.info("getMerchantStockList");

        logger.info("carrotPointMerchantMerchantId : {}", carrotPointMerchantStock.getCarrotPointMerchantMerchantId());
        logger.info("activateYn                    : {}", carrotPointMerchantStock.getActivateYn());

        return carrotPointMerchantStockService.getMerchantStockList(carrotPointMerchantStock);
    }

    /**
     * 모든 가맹점(가입업체) 상품 목록을 가져 온다.
     *
     * @param carrotPointMerchantStock
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getTotalMerchantStockList", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public List<CarrotPointMerchantStock> getTotalMerchantStockList
            (@RequestBody CarrotPointMerchantStock carrotPointMerchantStock) throws Exception {
        logger.info("getTotalMerchantStockList");

        logger.info("activateYn : ", carrotPointMerchantStock.getActivateYn());

        return carrotPointMerchantStockService.getTotalMerchantStockList(carrotPointMerchantStock);
    }

    /**
     * 가맹점(입점업체) 상품 내용을 가져 온다.
     *
     * @param carrotPointMerchantStock
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getMerchantStockInformation", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public CarrotPointMerchantStock getMerchantStockInformation
            (@RequestBody CarrotPointMerchantStock carrotPointMerchantStock) throws Exception {
        logger.info("getMerchantStockInformation");

        logger.info("idCarrotPointMerchantStock : {}", carrotPointMerchantStock.getIdCarrotPointMerchantStock());
        logger.info("activateYn : {}", carrotPointMerchantStock.getActivateYn());

        return carrotPointMerchantStockService.getMerchantStockInformation(carrotPointMerchantStock);
    }

    /**
     * 가맹점(입점업체) 상품 내용을 수정 한다.
     *
     * @param carrotPointMerchantStock
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/updateMerchantStockInformation", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public CarrotPointMerchantStock updateMerchantStockInformation
            (@RequestBody CarrotPointMerchantStock carrotPointMerchantStock) throws  Exception {
        logger.info("updateMerchantStockInformation");

        CarrotPointMerchantStock result = new CarrotPointMerchantStock();

        try {
            carrotPointMerchantStockService.updateMerchantStockInformation(carrotPointMerchantStock);
            result.setResult("SUCCESS");
            logger.info("success to update merchant stock information.");
        } catch (Exception e) {
            result.setResult("FAIL");
            logger.error("fail to update merchant stock information.");
        }

        return result;
    }

    /**
     * 가맹점(입점업체) 상품을 비활성화 한다.
     *
     * @param carrotPointMerchantStock
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/updateDeactivateMerchantStock", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public CarrotPointMerchantStock updateDeactivateMerchantStock
            (@RequestBody CarrotPointMerchantStock carrotPointMerchantStock) throws Exception {
        logger.info("updateDeactivateMerchantStock");

        CarrotPointMerchantStock result = new CarrotPointMerchantStock();

        try {
            carrotPointMerchantStockService.updateDeactivateMerchantStock(carrotPointMerchantStock);
            result.setResult("SUCCESS");
            logger.info("success to update deactivate merchant stock.");
        } catch (Exception e) {
            result.setResult("FAIL");
            logger.error("fail to update deactivate merchant stock.");
        }

        return result;
    }

    /**
     * 모든 가맹점(입점업체) 상품 검색 목록을 가져 온다.
     *
     * @param carrotPointMerchantStock
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getSearchMerchantStockList", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public List<CarrotPointMerchantStock> getSearchMerchantStockList
            (@RequestBody CarrotPointMerchantStock carrotPointMerchantStock) throws Exception {
        logger.info("getSearchMerchantStockList");

        logger.info("subject    : {}", carrotPointMerchantStock.getSubject());
        logger.info("category01 : {}", carrotPointMerchantStock.getCategory01());
        logger.info("category02 : {}", carrotPointMerchantStock.getCategory02());
        logger.info("category03 : {}", carrotPointMerchantStock.getCategory03());
        logger.info("category04 : {}", carrotPointMerchantStock.getCategory04());
        logger.info("category05 : {}", carrotPointMerchantStock.getCategory05());

        return carrotPointMerchantStockService.getSearchMerchantStockList(carrotPointMerchantStock);
    }
}
